/* Copyright (c) 2004-2024 Peigen.info. All rights reserved. */

package info.peigen.hotpot.component.gateway.dianlan.processor.business;

import com.alibaba.fastjson2.JSON;
import info.peigen.hotpot.component.gateway.core.util.HexUtil;
import info.peigen.hotpot.component.gateway.dianlan.annotations.DianlanProtocolParse;
import info.peigen.hotpot.component.gateway.dianlan.facade.command.order.DianlanStubBmsOrder;
import info.peigen.hotpot.component.gateway.dianlan.facade.command.result.DianlanStatusResult;
import info.peigen.hotpot.component.gateway.dianlan.processor.AbstractDianlanProcessor;
import info.peigen.hotpot.component.gateway.dianlan.protocol.Dianlan;
import info.peigen.hotpot.component.gateway.dianlan.protocol.passive.DianlanBmsAndLiquidCoolingUnit;
import lombok.extern.slf4j.Slf4j;
import org.noear.solon.annotation.Component;
import org.smartboot.socket.MessageProcessor;
import org.smartboot.socket.StateMachineEnum;
import org.smartboot.socket.transport.AioSession;

/**
 * <b>(dianlanStubBmsAndLiquidCoolingUnitResponseProcessor)</b>
 *
 * @author czj
 * @version 1.0.0
 * @since 2023/2/2
 */
@Component("dianlanStubBmsAndLiquidCoolingUnitResponseProcessor")
@Slf4j
public class DianlanStubBmsAndLiquidCoolingUnitResponseProcessor<T> extends AbstractDianlanProcessor implements MessageProcessor<T> {

    @Override
    public void process(AioSession session, Object obj) {
        Dianlan                        dianlan  = (Dianlan) obj;
        DianlanBmsAndLiquidCoolingUnit response = DianlanProtocolParse.deserialization(dianlan.getData(), DianlanBmsAndLiquidCoolingUnit.class);
        System.out.println(response);
        DianlanStubBmsOrder order = DianlanStubBmsOrder.builder()
                .stubNo(response.getStubNo())
                .chargingCount(response.getChargingCount())
                .stubType(response.getStubType())
                .chargingPortNo(response.getChargingPortNo())
                .batteryPackCapacitySOC(response.getBatteryPackStatusInfo().getBatteryPackCapacitySOC())
                .batteryPackStatusInfo(JSON.toJSONString(response.getBatteryPackStatusInfo()))
                .batteryInfo(JSON.toJSONString(response.getBatteryInfo()))
                .batteryTemperatureInfo(JSON.toJSONString(response.getBatteryTemperatureInfo()))
                .batteryInsulationResistanceInfo(JSON.toJSONString(response.getBatteryInsulationResistanceInfo()))
                .dianlanDcStubDataGram(JSON.toJSONString(response.getDianlanDcStubDataGram()))
                .dianlanDcStubForwardDataGram(JSON.toJSONString(response.getDianlanDcStubForwardDataGram()))
                .chargingSocketTemperatureDataGram(JSON.toJSONString(response.getChargingSocketTemperatureDataGram()))
                .batteryHighTemperatureBoxDataGram(JSON.toJSONString(response.getBatteryHighTemperatureBoxDataGram()))
                .waterCoolingUnitControlDataGram(JSON.toJSONString(response.getWaterCoolingUnitStatusDataGram()))
                .waterCoolingUnitStatusDataGram(JSON.toJSONString(response.getWaterCoolingUnitStatusDataGram()))
                .chineseDataGram(response.toString())
                .dataGram(HexUtil.binaryToHex(dianlan.getData()))
                .build();
        DianlanStatusResult result = dianlanServiceFacade.bmsResponse(order);
        log.info("Data：" + JSON.toJSONString(response));
    }

    @Override
    public void stateEvent(AioSession session, StateMachineEnum stateMachineEnum, Throwable throwable) {
        MessageProcessor.super.stateEvent(session, stateMachineEnum, throwable);
    }
}